/*   POZOR
     
     TOTO JE ZMRD !!!!!
*/     

/*
     MUNILIST   Listing magnitude and time from FITS, MAT files.
     Copyright (C) 1997  Filip Hroch, Masaryk University, Brno, CZ

     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program; if not, write to the Free Software
     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

     E-mail: hroch@physics.muni.cz
     Web:    http://www.sci.muni.cz/~hroch/index.html
     Postal address: F.Hroch
                     Department of Theoretical Physics and Astrophysics
                     Masaryk University
                     Kotlarska 2
                     611 37 Brno
                     Czech Republic
*/

#include <stdio.h>
#include <string.h>
#include <math.h>

int munilist_ (file,column,nstars,stars)
char *file;
int column,nstars,stars[];
{
FILE *f;
int i,j;
char *c;
double y,m,d,h,min,s,e,jd,datjd();
float x[200],dx[200];

if( (f = fopen(file,"rb")) == NULL )              /* Input FITS file */
  return(-1);
rfits(f,&y,&m,&d,&h,&min,&s,&e);        /*  read date, time and exposure */
jd = datjd(y,m,d + (h + (min + s/60.0)/60.0)/24.0);
jd = jd + e/2.0/86400.0;     /* Evaluate Julian date at half of exposure */
fclose(f);      

c = strstr(file,"fts");
if( c == NULL )
  c = strstr(file,"FTS");
sprintf(c,"MAT");
if( (f = fopen(file,"rb")) == NULL ) {
  sprintf(c,"mat");
  if( (f = fopen(file,"rb")) == NULL )
    return(-1); }

rdat(f,column,nstars,stars,x,dx);   /* Read magnitude data */

fclose(f);

for( i = 1; i <= nstars; i++ )
  if( fabs((double) dx[i]) < 1e-3 ){
    x[i] = 99.999;
    dx[i] = 9.999; }

fprintf(stdout,"%4d %02d %02d %02d %02d %02d %3d  %6.4lf ",
  (int) y,(int) m,(int) d,(int) h,(int) min,(int) s,(int) e,jd-2.4e6);
for(i = 1; i<= nstars; i++ )
  fprintf(stdout,"%7.3f%7.3f",x[i],dx[i]);
fprintf(stdout,"\n\r");

return(0);
}

/*CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC*/

rfits(f,y,m,d,h,min,s,e)
FILE *f;
double *y,*m,*d,*h,*min,*s,*e;
{
char line[81];

*y = 0.0; *m = 0.0; *d = 0.0; *h = 0.0; *min = 0.0; *s = 0.0; *e = 0.0;

while( fgets(line,81,f) ) {
  if( strstr(line,"DATE-OBS=") != NULL ) {
    sscanf(line + 11,"%2le%*c%2le%*c%2le",d,m,y);
    *y = *y + 1900.0; }
  else if( strstr(line,"UT      =") != NULL )
    sscanf(line + 11,"%2le%*c%2le%*c%2le",h,min,s);
  else if( strstr(line,"EXPTIME =") != NULL )
    sscanf(line + 11,"%le",e);
  else if( strstr(line,"END") != NULL ) 
    break;
}
}
      
/* CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC*/

rdat(f,column,nstars,stars,x,dx)
FILE *f;
int column,nstars,stars[];
float x[],dx[];
{
char lin[200];
int ap,find;
int i,j,in;

for( i = 1; i <= nstars; i++ )
  dx[i] = 0.0;

ap = 0;                                 /* Identification of file type */
fgets(lin,200,f);
if( strstr(lin," NL") != NULL ) {
  fgets(lin,200,f);
  sscanf(lin,"%d",&ap);
  ap = ap == 2;
}
fseek(f, 0L, SEEK_SET);
j = 0;
           
while( fgets(lin,200,f) ) {
 j++;
 in = 0;
 for( i = 1; i <= nstars; i++ )
    if( stars[i] == j ) {
      in = i;
      break; }
 if( in ) 
   if( ap ) {
     sscanf(lin+6+9*(column-2),"%f",&x[in]);
     fgets(lin,200,f);
     if( column == 4 )
       sscanf(lin+13+2*6,"%f",&dx[in]);
     else
       sscanf(lin+13+2*6+8+9*(column-4),"%f",&dx[in]);
     j++; }
   else
     sscanf(lin+6+9*(column-2),"%f %f",&x[in],&dx[in]);

}
}
